﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;

namespace FYPHP
{
    public partial class VirtualListing : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {
            SqlCommand cmdProduct, cmdCat;
            SqlDataReader dtrProduct, dtrCat;
            conn.Open();

            List<int> catid = new List<int>();

            cmdCat = new SqlCommand("SELECT v_id FROM Virtual_Shop v, Users u WHERE v.u_id=u.u_id AND u.u_status='Activate'", conn);
            dtrCat = cmdCat.ExecuteReader();

            if (dtrCat.HasRows)
            {
                while (dtrCat.Read())
                {
                    catid.Add(Convert.ToInt32(dtrCat["v_id"]));
                }
            }
            dtrCat.Close();

            String thead = "<table class='thumbs' width='100%' cellpadding='5' cellspacing='5' border='1' bordercolor='#aaa' align='center' style='border-collapse: collapse; text-align: center'>";
            String tbox = "";
            String tfoot = "</tr></table>";

            int c = catid.Count;
            int x = 0;

            for (int k = 0; k < catid.Count; k++)
            {
                cmdProduct = new SqlCommand("SELECT v_id, p_picture, username FROM Products p, Users u, Virtual_Shop v WHERE u.u_id=p.u_id AND u.u_id=v.u_id AND p_status='Approve' AND u_status='Activate' AND v.v_id='" + catid[k] + "' ORDER BY p_date DESC", conn);
                dtrProduct = cmdProduct.ExecuteReader();

                if (dtrProduct.HasRows)
                {
                    if (dtrProduct.Read())
                    {
                        tbox += "<td><a href='/VirtualShop.aspx?id=" + dtrProduct["v_id"] + "'><img src='/uploads/products/" + dtrProduct["p_picture"] + "' alt='' height='180px' width='180px' /></a><br />"
                                   + "<a href='/VirtualShop.aspx?id=" + dtrProduct["v_id"] + "'>" + dtrProduct["username"] + "'s Virtual Shop</a></td>";
                        x++;
                    }
                    if (x % 3 == 0)
                        tbox += "</tr><tr>";
                }
                dtrProduct.Close();
            }
            lblLay2.Text = thead + tbox + tfoot;

            conn.Close();
        }

        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }
    }
}